﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;

namespace CountTools
{
    public class RevitCommon
    {
        public static string ParameterToString(Parameter param)
        {
            string parameter = "none";

            if (param == null)
            {
                return parameter;
            }

            switch (param.StorageType)
            {
                case StorageType.Double:
                    double dVal = param.AsDouble();
                    parameter = dVal.ToString();
                    break;
                case StorageType.Integer:
                    int iVal = param.AsInteger();
                    parameter = iVal.ToString();
                    break;
                case StorageType.String:
                    string sVal = param.AsString();
                    parameter = sVal;
                    break;
                case StorageType.ElementId:
                    ElementId idVal = param.AsElementId();
                    parameter = idVal.IntegerValue.ToString();
                    break;
                case StorageType.None:
                    break;
            }
            return parameter;
        }

        /// <summary>
        /// 类型和类别过滤，返回IList<Element>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="document"></param>
        /// <param name="category"></param>
        /// <returns>IList<Element></returns>
        public static IList<Element> ClassAndCategoryFilterElements<T>(Document document, BuiltInCategory category)
        {
            FilteredElementCollector collector = new FilteredElementCollector(document);
            collector.OfCategory(category).OfClass(typeof(T));
            IList<Element> elements = collector.ToElements();
            return elements;
        }

        /// <summary>
        /// 类型和类别过滤，返回IList<ElementIds>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="document"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        public static ICollection<ElementId> ClassAndCategoryFilterElementIds<T>(Document document, BuiltInCategory category)
        {
            FilteredElementCollector collector = new FilteredElementCollector(document);
            collector.OfCategory(category).OfClass(typeof(T));
            ICollection<ElementId> elementIds = collector.ToElementIds();
            return elementIds;
        }
    }
}
